<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"chuangges.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Hybrid App、跨平台框架、微信公众号、移动端支付">
<meta property="og:type" content="article">
<meta property="og:title" content="移动端技术难点">
<meta property="og:url" content="https://chuangges.gitee.io/2019/06/28/8-4-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E9%9A%BE%E7%82%B9/index.html">
<meta property="og:site_name" content="闯哥要去流浪了">
<meta property="og:description" content="Hybrid App、跨平台框架、微信公众号、移动端支付">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://chuangges.gitee.io/images/webchat/mode-skip.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/mobile/payment.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/mobile/IAP.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/mobile/otherpay.png">
<meta property="og:image" content="https://chuangges.gitee.io/images/mobile/h5pay.png">
<meta property="article:published_time" content="2019-06-28T14:28:50.000Z">
<meta property="article:modified_time" content="2024-01-22T06:43:14.136Z">
<meta property="article:author" content="chuangges">
<meta property="article:tag" content="移动端开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://chuangges.gitee.io/images/webchat/mode-skip.png">

<link rel="canonical" href="https://chuangges.gitee.io/2019/06/28/8-4-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E9%9A%BE%E7%82%B9/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>移动端技术难点 | 闯哥要去流浪了</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="闯哥要去流浪了" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">闯哥要去流浪了</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">愿你岁月可回首，且以深情共白头</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://chuangges.gitee.io/2019/06/28/8-4-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E9%9A%BE%E7%82%B9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/chuang.png">
      <meta itemprop="name" content="chuangges">
      <meta itemprop="description" content="chuangge's blog.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="闯哥要去流浪了">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          移动端技术难点
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-06-28 22:28:50" itemprop="dateCreated datePublished" datetime="2019-06-28T22:28:50+08:00">2019-06-28</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2024-01-22 14:43:14" itemprop="dateModified" datetime="2024-01-22T14:43:14+08:00">2024-01-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">移动端开发</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>21k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>19 分钟</span>
            </span>
            <div class="post-description">Hybrid App、跨平台框架、微信公众号、移动端支付</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="一、微信公众号"><a href="#一、微信公众号" class="headerlink" title="一、微信公众号"></a>一、微信公众号</h1><h2 id="基础介绍"><a href="#基础介绍" class="headerlink" title="基础介绍"></a>基础介绍</h2><h3 id="公众号类型"><a href="#公众号类型" class="headerlink" title="公众号类型"></a>公众号类型</h3><blockquote>
<p>订阅号存放在一个文件夹中且需要点开查看关注，服务号直接显示在聊天列表。个人只能申请订阅号，几乎没有可用的接口。</p>
</blockquote>
<ul>
<li><strong>订阅号</strong>：主要用于推送资讯而让用户阅读，每天只能群发一条消息。</li>
<li><strong>服务号</strong>：主要用于为企业、政府等提供对用户的服务，比如银行的服务号可以让用户查询账单等，每个月只可群发四条消息。</li>
<li><strong>企业号</strong>：主要用于公司内部通讯使用，旨在为用户提供移动办公，需要先有成员的通讯信息验证才可以关注成功企业微信。</li>
<li><strong>测试号</strong>：用于开发测试及功能体验，是专门为开发人员准备的一种仅用于测试的公众号，但不可使用微信支付等高级功能。</li>
</ul>
<h3 id="微信公众平台"><a href="#微信公众平台" class="headerlink" title="微信公众平台"></a>微信公众平台</h3><blockquote>
<p>用于管理注册过的公众号，比如自定义菜单、自动回复、推送文章等，主要有两种管理模式：<strong>编辑模式</strong>就是为所有人提供的，如果你的需求仅仅只是最常见的菜单、自动回复等，使用编辑模式已经满足。但是如果你需求的功能比较复杂，就需要使用到<strong>开发模式</strong>。</p>
</blockquote>
<ul>
<li><strong>编辑模式</strong>：主要针对非编程人员及信息发布类公众帐号使用，开启后可以直接通过界面配置自定义菜单、自动回复的消息。优点是可视化界面配置、操作简单快捷，但功能有限。</li>
<li><strong>开发模式</strong>：主要针对开发人员使用，开启后可以使用微信公众平台的开放接口，但编辑模式的自定义菜单等功能会失效。优点是可以实现更多的复杂功能，提供个性化服务。</li>
</ul>
<h3 id="开发工具"><a href="#开发工具" class="headerlink" title="开发工具"></a>开发工具</h3><ul>
<li><strong>技术文档</strong>：<a target="_blank" rel="external nofollow noopener noreferrer" href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432/">微信公众平台开发文档</a>、<a target="_blank" rel="external nofollow noopener noreferrer" href="http://mp.weixin.qq.com/debug/">接口在线调试网址</a>。</li>
<li><strong>测试账号</strong>：拥有几乎所有的公众号接口但不支持微信支付，主要用于手机端开发测试。</li>
<li><strong>微信 API 在线调试</strong>：后端代码与微信服务器交互实现创建自定义菜单等功能，手机端可通过扫描测试号二维码查看最新效果。</li>
<li><strong>微信开发者工具</strong>：自带审查元素功能的微信浏览器，用于调试微信端页面。使用前需要绑定开发者微信号 (公众平台首页、开发者工具)。</li>
<li><strong>JS-SDK 工具包</strong>：基于微信内的网页开发工具包。开发者可用来借助微信高效地使用拍照、选图、语音、位置等手机系统的能力，而且可以直接使用微信分享、扫一扫、卡券、支付等微信特有的能力，为微信用户提供更优质的网页体验。</li>
</ul>
<h2 id="开发准备"><a href="#开发准备" class="headerlink" title="开发准备"></a>开发准备</h2><h3 id="公众号配置"><a href="#公众号配置" class="headerlink" title="公众号配置"></a>公众号配置</h3><blockquote>
<p>手机端调试：扫码关注测试二维码。公众号域名可以是 http&#x2F;https，小程序必须是 https。</p>
</blockquote>
<ol>
<li>注册一个服务号并认证通过：只有服务号可以开通微信支付。</li>
<li>管理员为开发者添加服务号和商户平台的用户管理权限：微信公众平台的权限为运营者微信号、微信商户平台的权限为员工账号。</li>
<li><strong>开通微信支付功能</strong><br>* 公众号：拥有通过微信认证的公众帐号且为服务号、服务号和商户平台是一个账户主体。<br>* 商户平台：产品中心处开通公众号支付、扫码支付等所需要的支付方式，然后配置支付授权目录。注意小程序也属于公众号支付，不需要单独开通。</li>
<li><strong>配置公众号</strong><br>* 测试账号：JS 接口安全域名 (用于实现微信分享等功能，不需要加 http&#x2F;https)、授权回调页面域名 (用于用户登录和基本信息获取，只支持 80 端口，务必使服务器监听 80 端口)、接口配置信息 (URL、Token 由后端提供)。<br>* IP 白名单：在公众号后台设置服务器 IP 为白名单，否则无法获取 access_token。<br>* 自定义菜单：上线时配置 公众号网址 + 各个菜单对应的前端路由，比如公司简介 <code>https://wx.shanghailife.com.cn/aboutus</code>。</li>
</ol>
<h3 id="内网穿透环境"><a href="#内网穿透环境" class="headerlink" title="内网穿透环境"></a>内网穿透环境</h3><blockquote>
<p>公众号开发涉及到微信的回调，所以本地调试的内网服务器需要外网服务器能够连接通信。解决方案主要有两种：一是<strong>购买域名和云服务器并配置代理</strong>，将本地服务器网址映射到外网。二是使用<strong>内网穿透技术</strong>，让本地服务器直接穿透到外网。我们一般通过拥有免费通道的 natapp 实现内网穿透，但缺点是外网访问本地项目的地址经常会变化而不太稳定，具体配置如下：</p>
</blockquote>
<ol>
<li>登录并购买免费隧道：首次需要<a target="_blank" rel="external nofollow noopener noreferrer" href="https://natapp.cn/register">注册</a>、点击购买并配置地址 <code>http://127.0.0.1:80</code>。</li>
<li>本机运行 web 服务：配置地址 <code>http://127.0.0.1:80</code>。</li>
<li>本机运行 natapp 服务：下载 <a target="_blank" rel="external nofollow noopener noreferrer" href="https://natapp.cn/#download">natapp.exe</a>、获取 authtoken (我的隧道处)。<br>* 直接启动：<strong>natapp -authtoken&#x3D;9ab6b9040a624f40</strong><br>* 通过 config.ini：<a target="_blank" rel="external nofollow noopener noreferrer" href="https://natapp.cn/article/config_ini">下载 config.ini</a> 并填入 authtoken 后双击启动。</li>
<li>测试 natapp 分配的网址：拔掉电脑网线后在浏览器地址栏打开即可。</li>
</ol>
<h3 id="接口参数"><a href="#接口参数" class="headerlink" title="接口参数"></a>接口参数</h3><ul>
<li><strong>appid</strong>：公众号唯一标识。</li>
<li><strong>secret</strong>：公众号开发密钥。</li>
<li><strong>openid</strong>：用户单平台的唯一标识，同一用户在公众号和小程序有不同的 openid。</li>
<li><strong>unionid</strong>：用户跨微信平台的唯一标识，同一用户在公众号和小程序有相同的 unionid。</li>
<li><strong>code</strong>：用户授权时用于获取 access_token，只能使用一次且 5分钟未被使用则自动过期。</li>
<li><strong>redirect_uri</strong>：完成授权后重定向的回调链接（其实是用户要访问的页面，注意转义）。</li>
<li><strong>response_type</strong> ：返回类型，固定为 code。</li>
<li><strong>state</strong>：重定向后被微信原样返回，可用于前后对比来防止别人的攻击。</li>
<li><strong>scope</strong>：授权方式。snsapi_base (不弹出授权页面而直接跳转，只能获取用户 openid)、snsapi_userinfo (弹出授权页面，可通过 openid 获取昵称、性别、所在地等信息)。</li>
<li><strong>#wechat_redirect</strong>：直接在微信打开链接的可选参数。实现页面 302 重定向时必须携带。</li>
</ul>
<h2 id="网页授权"><a href="#网页授权" class="headerlink" title="网页授权"></a>网页授权</h2><blockquote>
<p>用户通过扫码、点击按钮等方式进入公众号页面，没有用户信息时会跳转到微信授权页面，用户同意后则自动跳转到携带参数 code、state 的回调页面，然后通过 code 获取网页授权 access_token 后才能请求获取用户信息，详情可参考<a target="_blank" rel="external nofollow noopener noreferrer" href="https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html">官方文档</a>。注意区分 access_token：网页授权 access_token 不限制调用次数而且只要用户允许后就可以获取用户信息，普通 access_token 限制每日 2000 次而且必须关注公众号才能获取。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br></pre></td><td class="code"><pre><span class="line">后端接入：开源工具 weixin-java-tools  <span class="attr">https</span>:<span class="comment">//github.com/Wechat-Group/WxJava</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 1、pom.xml 引入 jar 包</span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.binarywang<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>weixin-java-mp<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.8.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2、application.yml 添加配置，没有则去申请测试账号</span></span><br><span class="line"><span class="attr">wechat</span>:</span><br><span class="line">  <span class="attr">mpAppId</span>: appid</span><br><span class="line">  <span class="attr">mpAppSecret</span>: appsecret</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3、新建读取配置文件 WechatMpProperties.java</span></span><br><span class="line">package com.<span class="property">hsc</span>.<span class="property">power</span>.<span class="property">dm</span>.<span class="property">wechat</span>.<span class="property">config</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.<span class="property">Data</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">boot</span>.<span class="property">context</span>.<span class="property">properties</span>.<span class="property">ConfigurationProperties</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">stereotype</span>.<span class="property">Component</span>;</span><br><span class="line"></span><br><span class="line">@<span class="title class_">Data</span></span><br><span class="line">@<span class="title class_">Component</span></span><br><span class="line">@<span class="title class_">ConfigurationProperties</span>(prefix = <span class="string">&quot;wechat&quot;</span>)</span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">WechatMpProperties</span> &#123;</span><br><span class="line">    private <span class="title class_">String</span> mpAppId;</span><br><span class="line">    private <span class="title class_">String</span> mpAppSecret;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 4、新建自定义微信配置 WechatMpConfig.java</span></span><br><span class="line">package com.<span class="property">hsc</span>.<span class="property">power</span>.<span class="property">dm</span>.<span class="property">wechat</span>.<span class="property">config</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">api</span>.<span class="property">WxMpService</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">api</span>.<span class="property">impl</span>.<span class="property">WxMpServiceImpl</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">config</span>.<span class="property">WxMpConfigStorage</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">config</span>.<span class="property">impl</span>.<span class="property">WxMpDefaultConfigImpl</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">beans</span>.<span class="property">factory</span>.<span class="property">annotation</span>.<span class="property">Autowired</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">context</span>.<span class="property">annotation</span>.<span class="property">Bean</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">stereotype</span>.<span class="property">Component</span>;</span><br><span class="line"></span><br><span class="line">@<span class="title class_">Component</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">WechatMpConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    @<span class="title class_">Autowired</span></span><br><span class="line">    private <span class="title class_">WechatMpProperties</span> wechatMpProperties;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 配置 WxMpService 所需信息</span></span><br><span class="line">    @<span class="title class_">Bean</span></span><br><span class="line">    public <span class="title class_">WxMpService</span> <span class="title function_">wxMpService</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="title class_">WxMpService</span> wxMpService = <span class="keyword">new</span> <span class="title class_">WxMpServiceImpl</span>();</span><br><span class="line">        <span class="comment">// 设置配置信息的存储位置</span></span><br><span class="line">        wxMpService.<span class="title function_">setWxMpConfigStorage</span>(<span class="title function_">wxMpConfigStorage</span>());</span><br><span class="line">        <span class="keyword">return</span> wxMpService;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 配置 appId、appsecret</span></span><br><span class="line">    @<span class="title class_">Bean</span></span><br><span class="line">    public <span class="title class_">WxMpConfigStorage</span> <span class="title function_">wxMpConfigStorage</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="title class_">WxMpDefaultConfigImpl</span> wxMpDefaultConfig = <span class="keyword">new</span> <span class="title class_">WxMpDefaultConfigImpl</span>();</span><br><span class="line">        wxMpDefaultConfig.<span class="title function_">setAppId</span>(wechatMpProperties.<span class="title function_">getMpAppId</span>());</span><br><span class="line">        wxMpDefaultConfig.<span class="title function_">setSecret</span>(wechatMpProperties.<span class="title function_">getMpAppSecret</span>());</span><br><span class="line">        <span class="keyword">return</span> wxMpDefaultConfig;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 5、新建微信用户 Bean</span></span><br><span class="line">package com.<span class="property">hsc</span>.<span class="property">power</span>.<span class="property">dm</span>.<span class="property">wechat</span>.<span class="property">vo</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.<span class="property">Data</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">bean</span>.<span class="property">result</span>.<span class="property">WxMpUser</span>;</span><br><span class="line"></span><br><span class="line">@<span class="title class_">Data</span></span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">WechatUser</span> &#123;</span><br><span class="line">    public <span class="title class_">WechatUser</span>(<span class="title class_">WxMpUser</span> wxMpUser, <span class="title class_">String</span> accessToken) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setAccessToken</span>(accessToken);</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setOpenid</span>(wxMpUser.<span class="title function_">getOpenId</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setUnionId</span>(wxMpUser.<span class="title function_">getUnionId</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setNickname</span>(wxMpUser.<span class="title function_">getNickname</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setLanguage</span>(wxMpUser.<span class="title function_">getLanguage</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setCountry</span>(wxMpUser.<span class="title function_">getCountry</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setProvince</span>(wxMpUser.<span class="title function_">getCity</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setCity</span>(wxMpUser.<span class="title function_">getCity</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setSex</span>(wxMpUser.<span class="title function_">getSex</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setSexDesc</span>(wxMpUser.<span class="title function_">getSexDesc</span>());</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setHeadImgUrl</span>(wxMpUser.<span class="title function_">getHeadImgUrl</span>());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="title class_">String</span> openid;</span><br><span class="line">    private <span class="title class_">String</span> accessToken;</span><br><span class="line">    private <span class="title class_">String</span> unionId;</span><br><span class="line">    private <span class="title class_">String</span> nickname;</span><br><span class="line">    private <span class="title class_">String</span> language;</span><br><span class="line">    private <span class="title class_">String</span> country;</span><br><span class="line">    private <span class="title class_">String</span> province;</span><br><span class="line">    private <span class="title class_">String</span> city;</span><br><span class="line">    private <span class="title class_">Integer</span> sex;</span><br><span class="line">    private <span class="title class_">String</span> sexDesc;</span><br><span class="line">    private <span class="title class_">String</span> headImgUrl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 6、授权接口 WechatController.java</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  /auth：获取授权跳转地址</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  /auth/user/info：初次授权获取用户信息</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@desp</span>  /token/user/info：静默授权获取用户信息</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">package com.<span class="property">hsc</span>.<span class="property">power</span>.<span class="property">dm</span>.<span class="property">wechat</span>.<span class="property">web</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.<span class="property">baomidou</span>.<span class="property">mybatisplus</span>.<span class="property">core</span>.<span class="property">toolkit</span>.<span class="property">ExceptionUtils</span>;</span><br><span class="line"><span class="keyword">import</span> com.<span class="property">hsc</span>.<span class="property">power</span>.<span class="property">core</span>.<span class="property">base</span>.<span class="property">ret</span>.<span class="property">Rb</span>;</span><br><span class="line"><span class="keyword">import</span> com.<span class="property">hsc</span>.<span class="property">power</span>.<span class="property">dm</span>.<span class="property">wechat</span>.<span class="property">vo</span>.<span class="property">WechatUser</span>;</span><br><span class="line"><span class="keyword">import</span> lombok.<span class="property">extern</span>.<span class="property">slf4j</span>.<span class="property">Slf4j</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">common</span>.<span class="property">api</span>.<span class="property">WxConsts</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">common</span>.<span class="property">error</span>.<span class="property">WxErrorException</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">api</span>.<span class="property">WxMpService</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">bean</span>.<span class="property">result</span>.<span class="property">WxMpOAuth2AccessToken</span>;</span><br><span class="line"><span class="keyword">import</span> me.<span class="property">chanjar</span>.<span class="property">weixin</span>.<span class="property">mp</span>.<span class="property">bean</span>.<span class="property">result</span>.<span class="property">WxMpUser</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">beans</span>.<span class="property">factory</span>.<span class="property">annotation</span>.<span class="property">Autowired</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">web</span>.<span class="property">bind</span>.<span class="property">annotation</span>.<span class="property">GetMapping</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">web</span>.<span class="property">bind</span>.<span class="property">annotation</span>.<span class="property">RequestMapping</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">web</span>.<span class="property">bind</span>.<span class="property">annotation</span>.<span class="property">RequestParam</span>;</span><br><span class="line"><span class="keyword">import</span> org.<span class="property">springframework</span>.<span class="property">web</span>.<span class="property">bind</span>.<span class="property">annotation</span>.<span class="property">RestController</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.<span class="property">net</span>.<span class="property">URLEncoder</span>;</span><br><span class="line"></span><br><span class="line">@<span class="title class_">Slf4</span>j</span><br><span class="line">@<span class="title class_">RestController</span></span><br><span class="line">@<span class="title class_">RequestMapping</span>(<span class="string">&quot;/wechat&quot;</span>)</span><br><span class="line">public <span class="keyword">class</span> <span class="title class_">WechatController</span> &#123;</span><br><span class="line">    @<span class="title class_">Autowired</span></span><br><span class="line">    private <span class="title class_">WxMpService</span> wxMpService;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取 code 参数：returnUrl 需要跳转的重定向 url</span></span><br><span class="line">    @<span class="title class_">GetMapping</span>(<span class="string">&quot;/auth&quot;</span>)</span><br><span class="line">    public <span class="title class_">Rb</span>&lt;<span class="title class_">String</span>&gt; <span class="title function_">authorize</span>(<span class="params">@RequestParam <span class="built_in">String</span> authCallbackUrl, </span></span><br><span class="line"><span class="params">    @RequestParam <span class="built_in">String</span> returnUrl</span>) &#123;</span><br><span class="line">        <span class="comment">// 暂时将回调地址硬编码在这里，方便调试</span></span><br><span class="line">        <span class="title class_">String</span> redirectUrl = wxMpService.<span class="title function_">oauth2buildAuthorizationUrl</span>(</span><br><span class="line">            authCallbackUrl, </span><br><span class="line">            <span class="title class_">WxConsts</span>.<span class="property">OAuth2Scope</span>.<span class="property">SNSAPI_USERINFO</span>, </span><br><span class="line">            <span class="title class_">URLEncoder</span>.<span class="title function_">encode</span>(returnUrl)</span><br><span class="line">        );</span><br><span class="line">        log.<span class="title function_">info</span>(<span class="string">&quot;【微信网页授权】获取code，redirectUrl = &#123;&#125;&quot;</span>, redirectUrl);</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Rb</span>.<span class="title function_">ok</span>(redirectUrl);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初次授权获取用户信息</span></span><br><span class="line">    @<span class="title class_">GetMapping</span>(<span class="string">&quot;/auth/user/info&quot;</span>)</span><br><span class="line">    public <span class="title class_">Rb</span>&lt;<span class="title class_">WechatUser</span>&gt; <span class="title function_">userInfo</span>(<span class="params">@RequestParam(<span class="string">&quot;code&quot;</span>) <span class="built_in">String</span> code, </span></span><br><span class="line"><span class="params">    @RequestParam(<span class="string">&quot;state&quot;</span>) <span class="built_in">String</span> returnUrl</span>) &#123;</span><br><span class="line">        <span class="title class_">WxMpOAuth2AccessToken</span> wxMpOAuth2AccessToken;</span><br><span class="line">        <span class="title class_">WxMpUser</span> wxMpUser;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 使用 code 换取 access_token 信息</span></span><br><span class="line">            wxMpOAuth2AccessToken = wxMpService.<span class="title function_">oauth2getAccessToken</span>(code);</span><br><span class="line">            wxMpUser = wxMpService.<span class="title function_">oauth2getUserInfo</span>(wxMpOAuth2AccessToken, <span class="literal">null</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (<span class="title class_">WxErrorException</span> e) &#123;</span><br><span class="line">            log.<span class="title function_">error</span>(<span class="string">&quot;【微信网页授权】异常，&#123;&#125;&quot;</span>, e);</span><br><span class="line">            <span class="keyword">throw</span> <span class="title class_">ExceptionUtils</span>.<span class="title function_">mpe</span>(e.<span class="title function_">getError</span>().<span class="title function_">getErrorMsg</span>());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 从 access_token 信息中获取到用户的 openid</span></span><br><span class="line">        <span class="title class_">String</span> openId = wxMpOAuth2AccessToken.<span class="title function_">getOpenId</span>();</span><br><span class="line">        log.<span class="title function_">info</span>(<span class="string">&quot;【微信网页授权】获取openId，openId = &#123;&#125;&quot;</span>, openId);</span><br><span class="line"></span><br><span class="line">        <span class="title class_">WechatUser</span> wechatUser = <span class="keyword">new</span> <span class="title class_">WechatUser</span>(wxMpUser, wxMpOAuth2AccessToken.<span class="title function_">getAccessToken</span>());</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Rb</span>.<span class="title function_">ok</span>(wechatUser);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 静默授权获取用户信息：openid、token</span></span><br><span class="line">    @<span class="title class_">GetMapping</span>(<span class="string">&quot;/token/user/info&quot;</span>)</span><br><span class="line">    public <span class="title class_">Rb</span>&lt;<span class="title class_">WechatUser</span>&gt; <span class="title function_">getUserInfo</span>(<span class="params">@RequestParam <span class="built_in">String</span> openid, </span></span><br><span class="line"><span class="params">    @RequestParam <span class="built_in">String</span> token</span>) &#123;</span><br><span class="line">        <span class="title class_">WxMpOAuth2AccessToken</span> wxMpOAuth2AccessToken = <span class="keyword">new</span> <span class="title class_">WxMpOAuth2AccessToken</span>();</span><br><span class="line">        wxMpOAuth2AccessToken.<span class="title function_">setOpenId</span>(openid);</span><br><span class="line">        wxMpOAuth2AccessToken.<span class="title function_">setAccessToken</span>(token);</span><br><span class="line">        <span class="comment">// 判断 accessToken是否失效，失效即刷新 accecssToken</span></span><br><span class="line">        boolean ret = wxMpService.<span class="title function_">oauth2validateAccessToken</span>(wxMpOAuth2AccessToken);</span><br><span class="line">        <span class="keyword">if</span> (!ret) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                wxMpOAuth2AccessToken = wxMpService.<span class="title function_">oauth2refreshAccessToken</span>(wxMpOAuth2AccessToken.<span class="title function_">getRefreshToken</span>());</span><br><span class="line">            &#125; <span class="keyword">catch</span> (<span class="title class_">WxErrorException</span> e) &#123;</span><br><span class="line">                log.<span class="title function_">error</span>(<span class="string">&quot;【微信网页授权】刷新token失败，&#123;&#125;&quot;</span>, e.<span class="title function_">getError</span>().<span class="title function_">getErrorMsg</span>());</span><br><span class="line">                <span class="keyword">throw</span> <span class="title class_">ExceptionUtils</span>.<span class="title function_">mpe</span>(e.<span class="title function_">getError</span>().<span class="title function_">getErrorMsg</span>());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取用户信息</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="title class_">WxMpUser</span> wxMpUser = wxMpService.<span class="title function_">oauth2getUserInfo</span>(wxMpOAuth2AccessToken, <span class="literal">null</span>);</span><br><span class="line">            <span class="title class_">WechatUser</span> wechatUser = <span class="keyword">new</span> <span class="title class_">WechatUser</span>(wxMpUser, wxMpOAuth2AccessToken.<span class="title function_">getAccessToken</span>());</span><br><span class="line">            <span class="keyword">return</span> <span class="title class_">Rb</span>.<span class="title function_">ok</span>(wechatUser);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (<span class="title class_">WxErrorException</span> e) &#123;</span><br><span class="line">            log.<span class="title function_">error</span>(<span class="string">&quot;【微信网页授权】获取用户信息失败，&#123;&#125;&quot;</span>, e.<span class="title function_">getError</span>().<span class="title function_">getErrorMsg</span>());</span><br><span class="line">            <span class="keyword">throw</span> <span class="title class_">ExceptionUtils</span>.<span class="title function_">mpe</span>(e.<span class="title function_">getError</span>().<span class="title function_">getErrorMsg</span>());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br></pre></td><td class="code"><pre><span class="line">前端接入</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1、环境配置 main.js</span></span><br><span class="line"><span class="keyword">let</span> appid = &#123;</span><br><span class="line">    <span class="attr">test</span>: <span class="string">&#x27;wx0431d286df253cea&#x27;</span>,</span><br><span class="line">    <span class="attr">uat</span>: <span class="string">&#x27;wx58656f9344fbc048&#x27;</span>,</span><br><span class="line">    <span class="attr">prd</span>: <span class="string">&#x27;wx7899e3317bf82ab8&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> domain = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"><span class="keyword">switch</span> (process.<span class="property">env</span>.<span class="property">NODE_ENV</span>) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;development&quot;</span>:</span><br><span class="line">        <span class="variable language_">window</span>.<span class="property">appid</span> = appid.<span class="property">test</span></span><br><span class="line">        domain = <span class="string">&quot;10.52.200.2:9011&quot;</span></span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;production&quot;</span>:</span><br><span class="line">        <span class="keyword">if</span> (location.<span class="property">href</span>.<span class="title function_">indexOf</span>(<span class="string">&#x27;test&#x27;</span>) &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">          <span class="variable language_">window</span>.<span class="property">appid</span> = appid.<span class="property">test</span></span><br><span class="line">          domain = <span class="string">&quot;shlife-test.zhongan.io&quot;</span></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (location.<span class="property">href</span>.<span class="title function_">indexOf</span>(<span class="string">&#x27;wxdat&#x27;</span>) &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">          <span class="variable language_">window</span>.<span class="property">appid</span> = appid.<span class="property">test</span></span><br><span class="line">          domain = <span class="string">&quot;wxdat.shanghailife.com.cn&quot;</span></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(location.<span class="property">href</span>.<span class="title function_">indexOf</span>(<span class="string">&#x27;wxpre&#x27;</span>) &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">          <span class="variable language_">window</span>.<span class="property">appid</span> = appid.<span class="property">uat</span></span><br><span class="line">          domain = <span class="string">&quot;wxpre.shanghailife.com.cn&quot;</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          <span class="variable language_">window</span>.<span class="property">appid</span> = appid.<span class="property">prd</span></span><br><span class="line">          domain = <span class="string">&quot;wx.shanghailife.com.cn&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    <span class="attr">default</span>:</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">&#125;</span><br><span class="line">axios.<span class="property">defaults</span>.<span class="property">baseURL</span> = location.<span class="property">protocol</span> + <span class="string">&quot;//&quot;</span> + domain</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2、路由拦截 router.js</span></span><br><span class="line">router.<span class="title function_">beforeEach</span>(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!to.<span class="property">meta</span>.<span class="property">noAuth</span>) &#123;   <span class="comment">// 需要授权的页面</span></span><br><span class="line">        <span class="keyword">if</span> (_.<span class="title function_">isEmpty</span>(store.<span class="property">getters</span>.<span class="property">wechatUserInfo</span>)) &#123;</span><br><span class="line">            <span class="keyword">if</span> (</span><br><span class="line">                !_.<span class="title function_">isEmpty</span>(store.<span class="property">getters</span>.<span class="property">openid</span>) &amp;&amp;</span><br><span class="line">                !_.<span class="title function_">isEmpty</span>(store.<span class="property">getters</span>.<span class="property">accessToken</span>)</span><br><span class="line">            ) &#123;</span><br><span class="line">                <span class="comment">// 存在 openid、accessToken 则已授权，判断是否过期，过期则刷新</span></span><br><span class="line">                store.<span class="title function_">dispatch</span>(<span class="string">&#x27;getUserInfo&#x27;</span>)</span><br><span class="line">                <span class="title function_">next</span>()</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 跳转到授权页面</span></span><br><span class="line">                <span class="variable language_">localStorage</span>.<span class="title function_">setItem</span>(<span class="string">&#x27;currentUrl&#x27;</span>, to.<span class="property">fullPath</span>)</span><br><span class="line">                <span class="title function_">next</span>(&#123; <span class="attr">name</span>: <span class="string">&#x27;auth&#x27;</span> &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="title function_">next</span>()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="title function_">next</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3、授权页面</span></span><br><span class="line">&#123;  </span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/auth&#x27;</span>,</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;auth&#x27;</span>,</span><br><span class="line">    <span class="attr">component</span>: <span class="function"><span class="params">resolve</span> =&gt;</span> &#123; <span class="built_in">require</span>([<span class="string">&#x27;@/views/auth/index.vue&#x27;</span>], resolve) &#125;,</span><br><span class="line">    <span class="attr">meta</span>: &#123; <span class="attr">noAuth</span>: <span class="literal">true</span> &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="attr">https</span>:<span class="comment">//open.weixin.qq.com/connect/oauth2/authorize?appid=$&#123;appid&#125;&amp;redirect_uri</span></span><br><span class="line">=$&#123;<span class="variable constant_">URI</span>&#125;&amp;response_type=code&amp;scope=$&#123;<span class="variable constant_">SCOPE</span>&#125;&amp;state=<span class="variable constant_">STATE</span>#wechat_redirect</span><br><span class="line"></span><br><span class="line">&lt;template&gt;&lt;/template&gt;</span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">    <span class="keyword">import</span> config <span class="keyword">from</span> <span class="string">&#x27;@/config&#x27;</span></span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">    <span class="keyword">import</span> <span class="title class_">WechatService</span> <span class="keyword">from</span> <span class="string">&#x27;@/api/wechat&#x27;</span></span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">    <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">      <span class="title function_">mounted</span>(<span class="params"></span>) &#123;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">          <span class="title class_">WechatService</span>.<span class="title function_">auth</span>(config.<span class="property">WechatAuthCallbackUrl</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">              <span class="keyword">if</span> (res.<span class="title function_">ok</span>()) &#123;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">                  <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span> = res.<span class="property">data</span></span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">              &#125;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">              </span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">          &#125;)</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">      &#125;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml">    &#125;</span></span></span><br><span class="line"><span class="language-javascript"><span class="language-xml"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 4、授权 store：在vuex中进行授权和存储用户信息</span></span><br><span class="line"><span class="keyword">import</span> _ <span class="keyword">from</span> <span class="string">&#x27;lodash&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">WechatService</span> <span class="keyword">from</span> <span class="string">&#x27;@/api/wechat&#x27;</span></span><br><span class="line"><span class="keyword">import</span> localStorageUtil <span class="keyword">from</span> <span class="string">&#x27;@/utils/LocalStorageUtil&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">state</span>: &#123;</span><br><span class="line">    <span class="attr">unionId</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    <span class="attr">openid</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    <span class="attr">accessToken</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    <span class="attr">wechatUserInfo</span>: &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">getters</span>: &#123;</span><br><span class="line">    <span class="attr">unionId</span>: <span class="function"><span class="params">state</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> state.<span class="property">unionId</span> || localStorageUtil.<span class="title function_">get</span>(<span class="string">&#x27;unionId&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">openid</span>: <span class="function"><span class="params">state</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> state.<span class="property">openid</span> || localStorageUtil.<span class="title function_">get</span>(<span class="string">&#x27;openid&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">accessToken</span>: <span class="function"><span class="params">state</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> state.<span class="property">accessToken</span> || localStorageUtil.<span class="title function_">get</span>(<span class="string">&#x27;accessToken&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">wechatUserInfo</span>: <span class="function"><span class="params">state</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> state.<span class="property">wechatUserInfo</span> || localStorageUtil.<span class="title function_">get</span>(<span class="string">&#x27;wechatUserInfo&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">mutations</span>: &#123;</span><br><span class="line">    <span class="comment">// 保存 userInfo，设置有效时间，默认30天</span></span><br><span class="line">    <span class="attr">saveWechatUserInfo</span>: <span class="function">(<span class="params">state, res</span>) =&gt;</span> &#123;</span><br><span class="line">        state.<span class="property">wechatUserInfo</span> = res</span><br><span class="line">        state.<span class="property">unionId</span> = res.<span class="property">unionId</span></span><br><span class="line">        state.<span class="property">openid</span> = res.<span class="property">openid</span></span><br><span class="line">        state.<span class="property">accessToken</span> = res.<span class="property">accessToken</span></span><br><span class="line">        localStorageUtil.<span class="title function_">set</span>(<span class="string">&#x27;unionId&#x27;</span>, res.<span class="property">unionId</span>)</span><br><span class="line">        localStorageUtil.<span class="title function_">set</span>(<span class="string">&#x27;openid&#x27;</span>, res.<span class="property">openid</span>)</span><br><span class="line">        localStorageUtil.<span class="title function_">set</span>(<span class="string">&#x27;accessToken&#x27;</span>, res.<span class="property">accessToken</span>)</span><br><span class="line">        localStorageUtil.<span class="title function_">set</span>(<span class="string">&#x27;wechatUserInfo&#x27;</span>, res, <span class="number">30</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">actions</span>: &#123;</span><br><span class="line">    <span class="comment">// 静默授权获取用户信息</span></span><br><span class="line">    <span class="keyword">async</span> <span class="title function_">getUserInfo</span>(<span class="params">&#123; commit, getters &#125;</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> openid = getters.<span class="property">openid</span></span><br><span class="line">        <span class="keyword">const</span> token = getters.<span class="property">accessToken</span></span><br><span class="line">        <span class="keyword">if</span> (!_.<span class="title function_">isEmpty</span>(openid) &amp;&amp; !_.<span class="title function_">isEmpty</span>(token)) &#123;</span><br><span class="line">            <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title class_">WechatService</span>.<span class="title function_">getUserInfo</span>(openid, token)</span><br><span class="line">            <span class="keyword">if</span> (res.<span class="title function_">ok</span>()) &#123;</span><br><span class="line">                <span class="title function_">commit</span>(<span class="string">&#x27;saveWechatUserInfo&#x27;</span>, res.<span class="property">data</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 初次授权获取用户信息</span></span><br><span class="line">    <span class="keyword">async</span> <span class="title function_">getAuthUserInfo</span>(<span class="params">&#123; commit &#125;, &#123; code, state &#125;</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!_.<span class="title function_">isEmpty</span>(code) &amp;&amp; !_.<span class="title function_">isEmpty</span>(state)) &#123;</span><br><span class="line">            <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="title class_">WechatService</span>.<span class="title function_">getAuthUserInfo</span>(code, state)</span><br><span class="line">            <span class="keyword">if</span> (res.<span class="title function_">ok</span>()) &#123;</span><br><span class="line">                <span class="title function_">commit</span>(<span class="string">&#x27;saveWechatUserInfo&#x27;</span>, res.<span class="property">data</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 4、自定义存储工具 localStorageUtil.js：用于设置保存有效期</span></span><br><span class="line"><span class="keyword">import</span> _ <span class="keyword">from</span> <span class="string">&#x27;lodash&#x27;</span></span><br><span class="line"><span class="keyword">import</span> moment <span class="keyword">from</span> <span class="string">&#x27;moment&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="title function_">get</span>(<span class="params">key, defaultValue</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">parse</span>(key, defaultValue)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="title function_">set</span>(<span class="params">key, obj, expires</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (expires) &#123;</span><br><span class="line">          <span class="keyword">const</span> tmpTime = <span class="title function_">moment</span>().<span class="title function_">add</span>(expires, <span class="string">&#x27;days&#x27;</span>).<span class="title function_">format</span>(<span class="string">&#x27;YYYY-MM-DD&#x27;</span>)</span><br><span class="line">          <span class="keyword">const</span> handleObj = &#123; <span class="attr">expires</span>: tmpTime, <span class="attr">value</span>: obj &#125;</span><br><span class="line">          <span class="variable language_">localStorage</span>.<span class="title function_">setItem</span>(key, <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(handleObj))</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          <span class="keyword">if</span> (_.<span class="title function_">isObject</span>(obj)) &#123;</span><br><span class="line">              <span class="variable language_">localStorage</span>.<span class="title function_">setItem</span>(key, <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(obj))</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              <span class="variable language_">localStorage</span>.<span class="title function_">setItem</span>(key, obj)</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="title function_">remove</span>(<span class="params">key</span>) &#123;</span><br><span class="line">      <span class="variable language_">localStorage</span>.<span class="title function_">removeItem</span>(key)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="title function_">parse</span>(<span class="params">key, defaultValue</span>) &#123;</span><br><span class="line">      <span class="keyword">let</span> value = <span class="variable language_">localStorage</span>.<span class="title function_">getItem</span>(key)</span><br><span class="line">      <span class="keyword">if</span> (_.<span class="title function_">isObject</span>(value)) &#123;</span><br><span class="line">          <span class="keyword">const</span> valueObj = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(value)</span><br><span class="line">          <span class="comment">// 有过期时间，判断是否过期：在现在时间之前，过期</span></span><br><span class="line">          <span class="keyword">if</span> (valueObj.<span class="property">expires</span>) &#123;</span><br><span class="line">              <span class="keyword">if</span> (<span class="title function_">moment</span>(valueObj.<span class="property">expires</span>).<span class="title function_">isBefore</span>(<span class="title function_">moment</span>(), <span class="string">&#x27;day&#x27;</span>)) &#123;</span><br><span class="line">                  <span class="variable language_">this</span>.<span class="title function_">remove</span>(key)</span><br><span class="line">                  <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">              &#125;</span><br><span class="line">              <span class="keyword">return</span> valueObj.<span class="property">value</span></span><br><span class="line">          &#125;</span><br><span class="line">          <span class="comment">// 没有过期时间直接返回对象</span></span><br><span class="line">          <span class="keyword">return</span> valueObj</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 不是对象则返回值</span></span><br><span class="line">      <span class="keyword">return</span> value || defaultValue</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="分享-x2F-支付"><a href="#分享-x2F-支付" class="headerlink" title="分享&#x2F;支付"></a>分享&#x2F;支付</h2><blockquote>
<p>公众号的内嵌 H5 页面实现微信分享、支付等功能需要引入 <strong>JS-SDK</strong>，而且必须先注入配置信息让微信验证是否支持。vue 项目注意：1、路由必须使用 history 模式，因为各个页面需要不同的签名信息，而且安卓设备的微信会自动截取 # 后面的内容而无法分享，2、iOS 需要使用第一次进入页面的 URL 获取签名，安卓每次路由切换都重新配置签名，具体如下：</p>
</blockquote>
  <div align="center">
      <img src="" data-original="/images/webchat/mode-skip.png" alt="页面跳转" width="100%">
  </div>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 安装</span></span><br><span class="line">npm install axios weixin-js-sdk -S</span><br><span class="line"></span><br><span class="line"><span class="comment">// wxSetting.js</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="keyword">import</span> wx <span class="keyword">from</span> <span class="string">&#x27;weixin-js-sdk&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// 初始化</span></span><br><span class="line">  wxConfig (callback) &#123;</span><br><span class="line">    <span class="comment">// 微信签名等配置数据需要从服务器获取 (url 参数需要编码)</span></span><br><span class="line">    <span class="keyword">let</span> url = <span class="built_in">encodeURIComponent</span>(location.<span class="property">href</span>.<span class="title function_">split</span>(<span class="string">&quot;#/&quot;</span>)[<span class="number">0</span>]);</span><br><span class="line">    axios.<span class="title function_">get</span>(<span class="string">`/api/getShareSign?url=<span class="subst">$&#123;url&#125;</span>`</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// 微信验证</span></span><br><span class="line">      <span class="keyword">const</span> &#123;appId, timestamp, nonceStr, signature&#125; = res.<span class="property">data</span></span><br><span class="line">      wx.<span class="title function_">config</span>(&#123;</span><br><span class="line">          <span class="attr">debug</span>: <span class="literal">true</span>,  <span class="comment">// 开启调试模式</span></span><br><span class="line">          appId,        <span class="comment">// 必填，公众号的唯一标识</span></span><br><span class="line">          timestamp,    <span class="comment">// 必填，生成签名的时间戳</span></span><br><span class="line">          nonceStr,     <span class="comment">// 必填，生成签名的随机串</span></span><br><span class="line">          signature,    <span class="comment">// 必填，签名 </span></span><br><span class="line">          <span class="attr">jsApiList</span>: [  <span class="comment">// 必填，需要使用的接口列表</span></span><br><span class="line">              <span class="string">&#x27;updateAppMessageShareData&#x27;</span>, </span><br><span class="line">              <span class="string">&#x27;updateTimelineShareData&#x27;</span>,</span><br><span class="line">              <span class="string">&#x27;scanQRCode&#x27;</span>,</span><br><span class="line">              <span class="string">&#x27;chooseImage&#x27;</span>,</span><br><span class="line">              <span class="string">&#x27;uploadImage&#x27;</span></span><br><span class="line">          ]</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="comment">// 验证成功：执行微信功能函数 share、scan</span></span><br><span class="line">      wx.<span class="title function_">ready</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">          callback &amp;&amp; <span class="title function_">callback</span>()</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="comment">// 验证失败</span></span><br><span class="line">      wx.<span class="title function_">error</span>(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">          <span class="variable language_">console</span>.<span class="title function_">error</span>(err.<span class="property">errMsg</span>)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 自定义分享</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@param</span> &#123;<span class="type">Object</span>&#125; data 分享内容</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@param</span> &#123;<span class="type">String</span>&#125; fullPath 分享路径，传入 this.$route.fullPath</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  <span class="title function_">share</span>(<span class="params">data, fullPath</span>) &#123;</span><br><span class="line">      <span class="keyword">const</span> &#123;title, imgUrl, desc&#125; = data</span><br><span class="line">      <span class="keyword">let</span> link = location.<span class="property">protocol</span> + <span class="string">&#x27;//&#x27;</span> + location.<span class="property">host</span> + fullPath</span><br><span class="line">      <span class="comment">// 分享到朋友圈：link 不需要编码但必须加 http://</span></span><br><span class="line">      wx.<span class="title function_">updateTimelineShareData</span>(&#123;</span><br><span class="line">          title,   <span class="comment">// 分享标题</span></span><br><span class="line">          imgUrl,  <span class="comment">// 分享图标</span></span><br><span class="line">          link,    <span class="comment">// 分享链接，域名必须与当前页面对应的公众号安全域名一致</span></span><br><span class="line">          <span class="attr">success</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="title class_">Toast</span>(<span class="string">&#x27;分享成功&#x27;</span>);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="comment">// 分享给朋友</span></span><br><span class="line">      wx.<span class="title function_">updateAppMessageShareData</span>(&#123;</span><br><span class="line">          title,</span><br><span class="line">          desc,</span><br><span class="line">          link,</span><br><span class="line">          imgUrl,</span><br><span class="line">          <span class="attr">success</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">              <span class="title class_">Toast</span>(<span class="string">&#x27;分享成功&#x27;</span>);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 禁止微信分享</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@desption</span> share(&#123; hideMenuItems: true &#125;, $route.fullPath)、hideOptionMenu()</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  <span class="title function_">hideOptionMenu</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">onBridgeReady</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">window</span>.<span class="property">WeixinJSBridge</span>.<span class="title function_">call</span>(<span class="string">&quot;hideOptionMenu&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="title class_">WeixinJSBridge</span> === <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">      <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">addEventListener</span>) &#123;</span><br><span class="line">        <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&quot;WeixinJSBridgeReady&quot;</span>, onBridgeReady, <span class="literal">false</span>);</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">attachEvent</span>) &#123;</span><br><span class="line">        <span class="variable language_">document</span>.<span class="title function_">attachEvent</span>(<span class="string">&quot;WeixinJSBridgeReady&quot;</span>, onBridgeReady);</span><br><span class="line">        <span class="variable language_">document</span>.<span class="title function_">attachEvent</span>(<span class="string">&quot;onWeixinJSBridgeReady&quot;</span>, onBridgeReady);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="title function_">onBridgeReady</span>();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 调用微信扫一扫接口</span></span><br><span class="line">  <span class="title function_">scan</span>(<span class="params"></span>)&#123;</span><br><span class="line">    wx.<span class="title function_">scanQRCode</span>(&#123;</span><br><span class="line">      <span class="attr">needResult</span>: <span class="number">0</span>,      <span class="comment">// 默认为 0，扫描结果由微信处理，1则直接返回扫描结果</span></span><br><span class="line">      <span class="attr">scanType</span>: [<span class="string">&quot;qrCode&quot;</span>,<span class="string">&quot;barCode&quot;</span>], <span class="comment">// 指定扫二维码还是一维码，默认二者都有</span></span><br><span class="line">      <span class="attr">success</span>: <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> result = res.<span class="property">resultStr</span>;  <span class="comment">// needResult 为 1 时，扫码返回的结果</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 图片上传</span></span><br><span class="line">  <span class="title function_">openCamera</span>(<span class="params"></span>)&#123;</span><br><span class="line">    wx.<span class="title function_">chooseImage</span>(&#123;</span><br><span class="line">      <span class="attr">count</span>: <span class="number">1</span>,              <span class="comment">// 默认9</span></span><br><span class="line">      <span class="attr">sizeType</span>: [<span class="string">&#x27;original&#x27;</span>, <span class="string">&#x27;compressed&#x27;</span>], <span class="comment">// 指定是原图还是压缩图，默认都有</span></span><br><span class="line">      <span class="attr">sourceType</span>: [<span class="string">&#x27;album&#x27;</span>, <span class="string">&#x27;camera&#x27;</span>],      <span class="comment">// 指定来源是相册还是相机，默认都有</span></span><br><span class="line">      <span class="attr">success</span>: <span class="keyword">function</span> (<span class="params">res</span>) &#123;</span><br><span class="line">          <span class="keyword">var</span> localIds = res.<span class="property">localIds</span>;</span><br><span class="line">          wx.<span class="title function_">uploadImage</span>(&#123;</span><br><span class="line">              <span class="attr">localId</span>: localIds.<span class="title function_">toString</span>(),  <span class="comment">// 图片 ID</span></span><br><span class="line">              <span class="attr">isShowProgressTips</span>: <span class="number">1</span>,         <span class="comment">// 进度提示</span></span><br><span class="line">              <span class="attr">success</span>: <span class="keyword">function</span> (<span class="params">res</span>) &#123; &#125;,</span><br><span class="line">              <span class="attr">fail</span>: <span class="keyword">function</span> (<span class="params">res</span>) &#123; &#125;</span><br><span class="line">          &#125;)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 微信支付：本质是对微信浏览器内置方法 weixinjsbridge.invoke() 的再次封装</span></span><br><span class="line">  <span class="title function_">wxPay</span>(<span class="params">options</span>)&#123;</span><br><span class="line">    <span class="keyword">const</span> &#123;appId, timeStamp, nonceStr, package, signType, paySign&#125; = options</span><br><span class="line">    wx.<span class="title function_">chooseWXPay</span>(&#123;</span><br><span class="line">        appId,</span><br><span class="line">        <span class="attr">timestamp</span>: timeStamp,  <span class="comment">// 支付签名时间戳，但新版支付后台返回 timeStamp</span></span><br><span class="line">        nonceStr,   <span class="comment">// 支付签名随机串</span></span><br><span class="line">        package,    <span class="comment">// 统一支付接口返回的参数值，提交格式如：prepay_id=\*\*\*</span></span><br><span class="line">        signType,   <span class="comment">// 签名方式，默认为&#x27;SHA1&#x27;，使用新版支付需传入&#x27;MD5&#x27;</span></span><br><span class="line">        paySign,    <span class="comment">// 支付签名</span></span><br><span class="line">        <span class="attr">success</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123; success &amp;&amp; <span class="title function_">success</span>() &#125;,</span><br><span class="line">        <span class="attr">cancel</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123; cancel &amp;&amp; <span class="title function_">cancel</span>(res) &#125;,  </span><br><span class="line">        <span class="attr">fail</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123; error &amp;&amp; <span class="title function_">error</span>(res) &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="二、移动端支付"><a href="#二、移动端支付" class="headerlink" title="二、移动端支付"></a>二、移动端支付</h1><blockquote>
<p>通过宿主环境主要可分为 APP 支付、微信支付、小程序支付、浏览器 H5 支付等，常见支付方式及其场景如下：</p>
</blockquote>
  <div align="center">
      <img src="" data-original="/images/mobile/payment.png" alt="支付方式" width="100%">
  </div>

<h2 id="APP-支付"><a href="#APP-支付" class="headerlink" title="APP 支付"></a>APP 支付</h2><ul>
<li><strong>前端工作</strong>：调用业务创建订单接口并获取订单 id、调用业务发起支付接口并获取支付信息、调用第三方支付提供的 SDK jsbridge 将支付信息传给第三方支付平台、成功调用 jsbridge 后就通过服务端轮询或 websocket 获取用户支付状态并然后更新页面。</li>
<li><strong>IAP 支付</strong>：In-App Purchase，即用户在 APP 内就能完成购买。IOS 强制使用有交易抽成的 AppStore，Android 需要开发 GooglePlay 等应用商店才能支持而但各个厂商不统一。</li>
<li><strong>第三方支付</strong>：跳转到支付宝等第三方 APP 才能完成支付行为，Android 大多数采用的方式。</li>
</ul>
  <div align="center">
      <img src="" data-original="/images/mobile/IAP.png" alt="IAP" width="100%">
  </div>

  <div align="center">
      <img src="" data-original="/images/mobile/otherpay.png" alt="第三方支付" width="100%">
  </div>


<h2 id="微信支付"><a href="#微信支付" class="headerlink" title="微信支付"></a>微信支付</h2><blockquote>
<p><a target="_blank" rel="external nofollow noopener noreferrer" href="https://pay.weixin.qq.com/wiki/doc/api/index.html">微信支付平台开发文档</a>：付款码支付、JSAPI 支付、Native 支付、APP 支付、H5 支付、小程序支付、刷脸支付。</p>
</blockquote>
  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// H5 支付流程：配置支付目录、获取用户标示 openid、用户支付并获取预支付信息、调用微信 jsapi</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">wxpay</span> (params, callback)&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="title class_">WeixinJSBridge</span> == <span class="string">&quot;undefined&quot;</span>) &#123;</span><br><span class="line">        <span class="comment">// _onBridge: onBridgeReady(params, callback)</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">addEventListener</span>)&#123;</span><br><span class="line">            <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;WeixinJSBridgeReady&#x27;</span>, _onBridge, <span class="literal">false</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">attachEvent</span>)&#123;</span><br><span class="line">            <span class="variable language_">document</span>.<span class="title function_">attachEvent</span>(<span class="string">&#x27;WeixinJSBridgeReady&#x27;</span>, _onBridge); </span><br><span class="line">            <span class="variable language_">document</span>.<span class="title function_">attachEvent</span>(<span class="string">&#x27;onWeixinJSBridgeReady&#x27;</span>, _onBridge);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="title function_">onBridgeReady</span>(params, callback);</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">onBridgeReady</span> (payParam, callback) &#123;   <span class="comment">// 预支付信息、支付回调函数</span></span><br><span class="line">    payParam = &#123;     </span><br><span class="line">        <span class="attr">appId</span>: <span class="string">&quot;wx2421b1c4370ec43b&quot;</span>,      <span class="comment">// 公众号 id，由商户传入 </span></span><br><span class="line">        <span class="attr">timeStamp</span>: <span class="string">&quot;1395712654&quot;</span>,          <span class="comment">// 时间戳，自1970年以来的秒数   </span></span><br><span class="line">        <span class="attr">nonceStr</span>: <span class="string">&quot;e61463f8efa94090&quot;</span>,     <span class="comment">// 随机字符串  </span></span><br><span class="line">        <span class="attr">package</span>: <span class="string">&quot;prepay_id=u802345jgf&quot;</span>,  <span class="comment">// 订单详情扩展字符串</span></span><br><span class="line">        <span class="attr">signType</span>: <span class="string">&quot;MD5&quot;</span>,                  <span class="comment">// 微信签名方式</span></span><br><span class="line">        <span class="attr">paySign</span>: <span class="string">&quot;70EA570631E4BB79&quot;</span>       <span class="comment">// 微信签名</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title class_">WeixinJSBridge</span>.<span class="title function_">invoke</span>(<span class="string">&#x27;getBrandWCPayRequest&#x27;</span>, payParam, <span class="keyword">function</span>(<span class="params">res</span>)&#123;  </span><br><span class="line">        <span class="title function_">callback</span>(res)</span><br><span class="line">        <span class="keyword">switch</span> (res.<span class="property">err_msg</span>) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;get_brand_wcpay_request:ok&quot;</span>:</span><br><span class="line">                <span class="comment">// 支付成功</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;get_brand_wcpay_request:fail&quot;</span>:</span><br><span class="line">                <span class="comment">// 支付失败</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">&quot;get_brand_wcpay_request:cancel&quot;</span>:</span><br><span class="line">                <span class="comment">// 取消支付</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 小程序支付流程：获取 openid、用户发起支付并获取预支付信息、小程序调用 wx.requestPayment</span></span><br><span class="line">wx.<span class="title function_">requestPayment</span>(&#123; </span><br><span class="line">    ...payParam, </span><br><span class="line">    <span class="attr">success</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123;&#125;, </span><br><span class="line">    <span class="attr">fail</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123;&#125;, </span><br><span class="line">    <span class="attr">complete</span>: <span class="keyword">function</span>(<span class="params">res</span>) &#123;&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<h2 id="H5-支付"><a href="#H5-支付" class="headerlink" title="H5 支付"></a>H5 支付</h2><blockquote>
<p>从手机浏览器直接唤起微信或支付宝进行支付，也通过生成二维码让用户扫描支付。注意嵌入到小程序的 h5 页面不支持 h5 原生支付，但可以通过 window.wx.miniProgram.navigateTo 跳转到小程序页面支付。以下为拉起微信支付的流程图：</p>
</blockquote>
  <div align="center">
      <img src="" data-original="/images/mobile/h5pay.png" alt="H5 支付" width="100%">
  </div>

  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@title</span> 中间页</span></span><br><span class="line"><span class="comment"> * https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?</span></span><br><span class="line"><span class="comment"> prepay_id=wx162128390473563d952e4f371ca7d70000&amp;package=2985141145&amp;</span></span><br><span class="line"><span class="comment"> redirect_url=https%3A%2F%2Fwww.ggl.cn%2Feggl%2Fh5%2Fsale%2Fpay%2Fresult</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> prepay_id、package：支付相关参数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> redirect_url：支付成功、取消、超时返回浏览器要打开的页面地址</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@prame</span> deeplink:</span></span><br><span class="line"><span class="comment"> * weixin://wap/pay?prepayid%3Dwx162128390473563d952e4f371ca7d70000</span></span><br><span class="line"><span class="comment"> &amp;package=2985141145&amp;noncestr=1623850119&amp;sign=bfc799bb3c643e44c0a334b53</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">goWechatPayH5</span> () &#123;</span><br><span class="line">    <span class="keyword">let</span> _this = <span class="variable language_">this</span>;</span><br><span class="line">    <span class="keyword">let</span> cur_url = <span class="built_in">encodeURI</span>(<span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span> + <span class="string">&#x27;?openLayer=1&#x27;</span>)</span><br><span class="line">    <span class="title function_">axios</span>(&#123; </span><br><span class="line">        <span class="attr">url</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        <span class="attr">method</span>: <span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">        <span class="attr">data</span>: <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(prames)</span><br><span class="line">    &#125;).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (res.<span class="property">code</span> == <span class="number">200</span>) &#123;</span><br><span class="line">            <span class="keyword">let</span> mweb_url = res.<span class="property">data</span>.<span class="property">mweb_url</span></span><br><span class="line">            <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span> = mweb_url + <span class="string">&#x27;&amp;redirect_url=&#x27;</span> + cur_url;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res.<span class="property">code</span> === <span class="number">400</span>) &#123;</span><br><span class="line">            _this.$toast(res.<span class="property">error</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
















    </div>

    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;margin-top:20px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>chuangges
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://chuangges.gitee.io/2019/06/28/8-4-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E9%9A%BE%E7%82%B9/" title="移动端技术难点">https://chuangges.gitee.io/2019/06/28/8-4-移动端难点/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh_CN" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/06/24/8-1-%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%BC%80%E5%8F%91/" rel="prev" title="移动端开发">
      <i class="fa fa-chevron-left"></i> 移动端开发
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/07/07/9-1-%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%85%A5%E9%97%A8/" rel="next" title="微信小程序入门">
      微信小程序入门 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="nav-number">1.</span> <span class="nav-text">一、微信公众号</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E7%A1%80%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.</span> <span class="nav-text">基础介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%AC%E4%BC%97%E5%8F%B7%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.1.1.</span> <span class="nav-text">公众号类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%B9%B3%E5%8F%B0"><span class="nav-number">1.1.2.</span> <span class="nav-text">微信公众平台</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7"><span class="nav-number">1.1.3.</span> <span class="nav-text">开发工具</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%80%E5%8F%91%E5%87%86%E5%A4%87"><span class="nav-number">1.2.</span> <span class="nav-text">开发准备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%AC%E4%BC%97%E5%8F%B7%E9%85%8D%E7%BD%AE"><span class="nav-number">1.2.1.</span> <span class="nav-text">公众号配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E7%8E%AF%E5%A2%83"><span class="nav-number">1.2.2.</span> <span class="nav-text">内网穿透环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E5%8F%82%E6%95%B0"><span class="nav-number">1.2.3.</span> <span class="nav-text">接口参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83"><span class="nav-number">1.3.</span> <span class="nav-text">网页授权</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E4%BA%AB-x2F-%E6%94%AF%E4%BB%98"><span class="nav-number">1.4.</span> <span class="nav-text">分享&#x2F;支付</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E7%A7%BB%E5%8A%A8%E7%AB%AF%E6%94%AF%E4%BB%98"><span class="nav-number">2.</span> <span class="nav-text">二、移动端支付</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#APP-%E6%94%AF%E4%BB%98"><span class="nav-number">2.1.</span> <span class="nav-text">APP 支付</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98"><span class="nav-number">2.2.</span> <span class="nav-text">微信支付</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#H5-%E6%94%AF%E4%BB%98"><span class="nav-number">2.3.</span> <span class="nav-text">H5 支付</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="chuangges" src="" data-original="/images/chuang.png">
  <p class="site-author-name" itemprop="name">chuangges</p>
  <div class="site-description" itemprop="description">chuangge's blog.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chuangges" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chuangges" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-stack-overflow fa-fw"></i>StackOverflow</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://juejin.im/" title="https:&#x2F;&#x2F;juejin.im&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">掘金</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.nowcoder.com/" title="https:&#x2F;&#x2F;www.nowcoder.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">牛客网</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.docschina.org/" title="https:&#x2F;&#x2F;www.docschina.org&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">印记中文</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://tool.lu/" title="http:&#x2F;&#x2F;tool.lu&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">在线工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tinypng.com/" title="https:&#x2F;&#x2F;tinypng.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片压缩</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://bigjpg.com/" title="https:&#x2F;&#x2F;bigjpg.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">图片放大</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tool.chinaz.com/" title="https:&#x2F;&#x2F;tool.chinaz.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">站长工具</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://learn-anything.xyz/" title="https:&#x2F;&#x2F;learn-anything.xyz&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Learn-Anything</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stackoverflow.com/" title="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="external nofollow noopener noreferrer" target="_blank">Stack-Overflow</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chuangge</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">1m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">15:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="external nofollow noopener noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"position":"left","width":180,"height":360},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script>
        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(e){e.imageLazyLoadSetting.processImages=t;var n=e.imageLazyLoadSetting.isSPA,i=e.imageLazyLoadSetting.preloadRatio||1,r=o();function o(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){n&&(r=o());for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(e.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];e=function(){r=r.filter(function(t){return o!==t})},(t=o).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,t.removeAttribute("data-original"),e&&e()},t.src!==i&&(n.src=i))}()}function a(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",a),e.addEventListener("resize",a),e.addEventListener("orientationchange",a)}(this);</script></body>
</html>
